home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Linked List Template Classes / Linked Lists Ä.sit / Linked Lists ƒ / source code / main.cp < prev    next >
Text File  |  1995-03-03  |  5KB  |  227 lines

  1. #include "SimpleWindowClass.h"
  2. #include "LinkedLists.h"
  3.  
  4. #define rMenuBar 128
  5. #define rWindow 128
  6. #define mApple 128
  7. #define mFile 129
  8. #define rAboutDialog 128
  9. #define    iAbout 1
  10. #define iNewWindow 1
  11. #define    iCloseWindow 2
  12. #define iQuit 4
  13.  
  14. void    InitMac();
  15. void    DoCommand(long    theCommand);
  16. void    DoMouseDown(EventRecord *theEvent);
  17. void    EventLoop();
  18.  
  19.  
  20. /*
  21.     Create a SlimPlusClass List with a SimpleWindowClass class and a WindowPtr to use
  22.     for comparisons
  23.     
  24.     tempWindow is used throughout the program to create a new window
  25.     quit is used to know when to quit the program
  26.     windowNumber is the number of the window created
  27. */
  28.  
  29.  
  30. SlimPlusClass<SimpleWindowClass, WindowPtr> myWindowList;
  31. SimpleWindowClass    *tempWindow;
  32. short                windowNumber=0;
  33. Boolean                quit=0;
  34.  
  35. void main()
  36. {
  37.     extern    SlimPlusClass<SimpleWindowClass, WindowPtr> myWindowList;
  38.     extern    SimpleWindowClass    *tempWindow;
  39.     extern    Boolean    quit;
  40.     extern    short    windowNumber;
  41.     
  42.     
  43.     InitMac();        //    Do standard Mac Initializations, plus set up menu bar
  44.     
  45.     
  46.     //    Create new window, then push it on the list
  47.     
  48.     windowNumber++;
  49.     tempWindow=new SimpleWindowClass();
  50.     tempWindow->CreateWindow(windowNumber, rWindow);
  51.     myWindowList.Push(*tempWindow);
  52.     
  53.     
  54.     //    Enter Event Loop
  55.     while (!quit)
  56.         EventLoop();
  57.     
  58.     
  59.     //    When quitting, delete all entries in list.  This is not actually necessary, because
  60.     //    the destructor in the SlimPlusClass will call the function, too
  61.         
  62.     myWindowList.DeleteAll();
  63.  
  64. }
  65.  
  66.  
  67.  
  68. void    InitMac()
  69. {
  70.     InitGraf(&thePort);
  71.     InitFonts();
  72.     InitWindows();
  73.     InitMenus();
  74.     TEInit();
  75.     InitDialogs(0L);
  76.     InitCursor();
  77.     MaxApplZone();
  78.     
  79.     
  80.     //    Get menu bar, add apple menu items, draw menu bar
  81.     
  82.     Handle    hmenuBar;
  83.     MenuHandle hmenu;
  84.     
  85.     hmenuBar=GetNewMBar(rMenuBar);
  86.     SetMenuBar(hmenuBar);
  87.     DisposeHandle(hmenuBar);
  88.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');
  89.     DrawMenuBar();
  90.  
  91. }
  92.  
  93. void    EventLoop()
  94. {
  95.     EventRecord    theEvent;
  96.     
  97.     //    This event loop only processes keyDown, mouseDown, and update and activate events
  98.     //    No time is needed for background processing, (i.e., null events), so sleep time is
  99.     //    set to 0xFFFFFFFF
  100.     
  101.     if (WaitNextEvent(everyEvent, &theEvent, 0xFFFFFFFF, nil)) {
  102.     
  103.         switch (theEvent.what) {
  104.             case keyDown:
  105.                 if (theEvent.modifiers & cmdKey) {
  106.                     DoCommand(MenuKey(theEvent.message));
  107.                     HiliteMenu(0);
  108.                 }
  109.             break;
  110.             
  111.             case mouseDown:
  112.                 DoMouseDown(&theEvent);
  113.             break;
  114.             
  115.     /*****
  116.         The update and activate events are a good example of how SlimPlusClass's XInList
  117.         function is used.  Passed a WindowPtr and a pointer to a SimpleWindowClass pointer, it
  118.         will return whether the WindowPtr is in the list and a pointer to the object containing
  119.         the WindowPtr in the second paramater.
  120.     *****/
  121.             case updateEvt:
  122.             case activateEvt:
  123.                 SimpleWindowClass    *thisObject;
  124.                 
  125.                 if (myWindowList.XInList((WindowPtr) theEvent.message, thisObject)) {
  126.                     if (updateEvt)
  127.                         thisObject->UpdateWindow();
  128.                     else
  129.                         thisObject->ActivateWindow(theEvent.modifiers & activeFlag);
  130.                 }
  131.             break;
  132.             
  133.             
  134.         }
  135.     }
  136. }
  137.  
  138. void    DoCommand(long theCommand)
  139. {
  140.     short    menu=HiWord(theCommand), item=LoWord(theCommand);
  141.     extern    short windowNumber;
  142.     extern    Boolean quit;
  143.     extern    SimpleWindowClass *tempWindow;
  144.     
  145.     HiliteMenu(0);
  146.     
  147.     switch (menu) {
  148.         case mApple:
  149.             if (item==iAbout) {
  150.                 DialogPtr    aboutDialog;
  151.                 
  152.                 aboutDialog=(DialogPtr) NewPtr(sizeof (DialogRecord));
  153.                 aboutDialog=GetNewDialog(rAboutDialog, aboutDialog, (WindowPtr) -1L);
  154.                 ModalDialog(nil, nil);
  155.                 CloseDialog(aboutDialog);
  156.                 DisposePtr((Ptr) aboutDialog);
  157.             }
  158.             
  159.             else {
  160.                 Str255 sName;
  161.                 GetItem(GetMenu(mApple), item, sName);
  162.                 OpenDeskAcc(sName);
  163.             }
  164.         break;
  165.         
  166.         case mFile:
  167.             switch (item) {
  168.                 case iNewWindow:
  169.                     windowNumber++;
  170.                     tempWindow=new SimpleWindowClass();
  171.                     tempWindow->CreateWindow(windowNumber, rWindow);
  172.                     myWindowList.Push(*tempWindow);
  173.                 break;
  174.                 
  175.                 case iCloseWindow:
  176.                     WindowPtr    theWindow=FrontWindow();
  177.                     myWindowList.XInList(theWindow, tempWindow);
  178.                     tempWindow->Hide();
  179.                 break;
  180.                 
  181.                 case iQuit:
  182.                     quit=1;
  183.                 break;
  184.             }
  185.         break;
  186.     }
  187. }
  188.  
  189. void    DoMouseDown(EventRecord *theEvent)
  190. {
  191.     WindowPtr    theWindow;
  192.     short        windowPart;
  193.     extern        SlimPlusClass<SimpleWindowClass, WindowPtr> myWindowList;
  194.     
  195.     windowPart=FindWindow(theEvent->where, &theWindow);
  196.     
  197.     
  198.     /*****
  199.         After finding which window is involved in the mouseDown event, retrieve the object
  200.         containing the WindowPtr and call the proper function.  To make things simple, and
  201.         because this ListClass doesn't have a delete function for a specific object, we'll
  202.         just hide the window if the user closes it.
  203.     *****/
  204.     
  205.     myWindowList.XInList(theWindow, tempWindow);
  206.     
  207.     
  208.     switch (windowPart) {
  209.         case inGoAway:
  210.             if (TrackGoAway(theWindow, theEvent->where))
  211.                 tempWindow->Hide();
  212.         break;
  213.         
  214.         case inDrag:
  215.             tempWindow->Drag(theEvent->where);
  216.         break;
  217.         
  218.         case inContent:
  219.             if (FrontWindow()!=theWindow)
  220.                 SelectWindow(theWindow);
  221.         break;
  222.         
  223.         case inMenuBar:
  224.             DoCommand(MenuSelect(theEvent->where));
  225.         break;
  226.     }
  227. }